package com.cgl.service.imp;

import com.cgl.mapper.ArticleMapper;
import com.cgl.mapper.CommentsMapper;
import com.cgl.pojo.Comment;
import com.cgl.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
public class CommentServiceImp implements CommentService {
    @Autowired
    private CommentsMapper commentsMapper;
    @Autowired
    private ArticleMapper articleMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addComments(Comment comment) {
        String id = UUID.randomUUID().toString().replace("-", "");
        comment.setId(id);

        String articleId = comment.getArticleId();
        if (!articleMapper.addCommentCount(articleId)) return false;

        comment.setCreatedTime(System.currentTimeMillis());
        comment.setStatus("0");
        return commentsMapper.addComments(comment);
    }

    @Override
    public Boolean updateStatus(String id, String status) {
        return commentsMapper.updateStatus(id,status);
    }

    @Override
    public List<Comment> selectAll(String articleId, int pageIndex, int pageSize) {
        int start = (pageIndex-1)*pageSize;
        return commentsMapper.selectAll(articleId,start,pageSize);
    }
}
